﻿using MediatR;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.BinLocationCommands;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.BinLocationCommandsHandlder
{
    public class BinLocationSelectNameCommandHandlder : IRequestHandler<BinLocationSelectNameCommand, ApiResult<List<WBDTO>>>
    {
        private readonly ILogger<BinLocationSelectNameCommandHandlder> _logger;
        private readonly IBinLocationRepository _binLocationRepository;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public BinLocationSelectNameCommandHandlder(ILogger<BinLocationSelectNameCommandHandlder> logger, IBinLocationRepository binLocationRepository, IWarehouseInfoRepository warehouseInfoRepository)
        {
            _logger = logger;
            _binLocationRepository = binLocationRepository;
            _warehouseInfoRepository = warehouseInfoRepository;
        }
        public async Task<ApiResult<List<WBDTO>>> Handle(BinLocationSelectNameCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<WBDTO>> result = new ApiResult<List<WBDTO>>();
            try
            {
                _logger.LogInformation("显示库位类型成功");
                var blist = await _binLocationRepository.GetAllAsync();
                var list = (from a in blist
                            join b in await _warehouseInfoRepository.GetAllAsync()
                            on a.WarehouseId equals b.WarehouseId
                            where a.IsDelete==false && b.IsDelete == false
                            select new WBDTO
                            {
                                WarehouseId=b.WarehouseId,
                                WarehouseNo=b.WarehouseNo,
                                WarehouseName=b.WarehouseName,
                                BinLocationId=a.BinLocationId  ,
                                BinLocationNo=a.BinLocationNo,
                                BinLocationName=a.BinLocationName,
                            }


                    ).ToList();
                result.Data = list;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
